﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.IO;
using Common;

namespace webs.website_admin.knowlegebase
{
    public partial class bannerconfiglist : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = -1;
            PageBegin();

            switch (RequestBase.GetFormString("otype"))
            {
                case "loadingdata":
                    LoadingData();
                    break;
                case "deleteitems":
                    Delete();
                    break;
            }
        }

        #region 信息绑定
        void LoadingData()
        {
            string ctrl = "~/website_admin/knowlegebase/_controls/ctlPageConfig.ascx";

            string SQL = " ";

            if (!string.IsNullOrEmpty(RequestBase.GetFormString("title")))
            {
                SQL += " AND Text1 LIKE '%" + RequestBase.GetFormString("title") + "%'";
            }  

            Control uc_rep = LoadControl(ctrl);

            Repeater rptBannerConfigList = (Repeater)uc_rep.FindControl("rptBannerConfigList");

            DBUtility.Pagination pagination = new DBUtility.Pagination();
            try
            {
                if (!string.IsNullOrEmpty(RequestBase.GetString("index")))
                    pagination.Sequence = Convert.ToInt32(RequestBase.GetString("index"));
                else
                    pagination.Sequence = 1;

                pagination.ItemUnit = "条记录";
                pagination.SortSentence = " ORDER BY ShowPositionCode ASC";
                pagination.Primarykey = "ConfigId";
                pagination.SearchField = "*";
                pagination.TableName = "PageConfigBase";
                if (!string.IsNullOrEmpty(RequestBase.GetString("pagesize")))
                    pagination.PageSize = Convert.ToInt32(RequestBase.GetString("pagesize"));
                else
                    pagination.PageSize = 20;
                pagination.UrlStr = "";
                pagination.ImagesPath = "../_imgs/grid/";
                pagination.SearchSentence = SQL.Trim();

                rptBannerConfigList.DataSource = pagination.GetDataTable();
                rptBannerConfigList.DataBind();

                pagination.PageBarFlexigrid();
                uc_rep.GetType().GetProperty("PaginationBar").SetValue(uc_rep, pagination.PageBar, null);

                StringBuilder strb = new StringBuilder();
                StringWriter sw = new StringWriter(strb);
                HtmlTextWriter htw = new HtmlTextWriter(sw);

                rptBannerConfigList.RenderControl(htw);

                BLL.AjaxHelper.ResponseWrite(strb.ToString());
            }
            catch (System.Threading.ThreadAbortException err)
            {
                //nothing   to   do   
            }
            catch (Exception err)
            {
                BLL.AjaxHelper.ResponseWrite(err.Message);
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除目录
        /// </summary>
        void Delete()
        {
            string itemsid = RequestBase.GetFormString("itemsid").Replace("'", "").Replace("'", "");

            string res = "";
            foreach (string objectid in itemsid.Split(','))
            {
                RemoveAttachment(objectid);

                /// 逻辑删除商品记录
                BLL.PageConfig.BannerConfig.Delete(objectid);

                res = "succeeded";
            }

            BLL.AjaxHelper.ResponseWrite(res);
        }
        void RemoveAttachment(string objectid)
        {
            Model.PageConfig.BannerConfig _obj = BLL.PageConfig.BannerConfig.GetObjectById(objectid);

            string _filepath = "";

            _filepath = ConfigurationManager.AppSettings["BannerImages"].ToString();

            if (_obj.Picture1.Split('*').Length > 1 && _obj.Picture1.Split('*')[1] != "")
            {
                if (File.Exists(Server.MapPath("../../" + _filepath + _obj.Picture1.Split('*')[1])))
                {
                    File.Delete(Server.MapPath("../../" + _filepath + _obj.Picture1.Split('*')[1]));
                }
            } 
        }
        #endregion
    }
}
